package indexer;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author Mauricio Silclir This class manage the serialization of other objects, creating
 * the files in disk and accessing them when required. It works by providing serialization
 * of SerializableHawar objects, which is an abstract class.
 */
public class ManageSerializing {
	private static String FILES_DIR = "src/indexer/files/";
	private File f;
	private FileOutputStream lowLevelOut;
	private ObjectOutputStream highLevelOut;
	private FileInputStream lowLevelIn;
	private ObjectInputStream highLevelIn;
	private boolean ok;

	public ManageSerializing() {
	}

	public SerializableHawar readFile(String fileName) {
		SerializableHawar a = null;
		ok = true;
		try {
			f = new File(ManageSerializing.FILES_DIR + fileName);
			lowLevelIn = new FileInputStream(f);
			highLevelIn = new ObjectInputStream(lowLevelIn);
			a = (SerializableHawar) highLevelIn.readObject();
			lowLevelIn.close();
		} catch (IOException ex) {
			ex.printStackTrace();
			ok = false;
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
			ok = false;
		}
		return a;
	}

	public void writeFile(SerializableHawar a) {
		if (a.getFileName() != null) {
			ok = true;
			try {
				f = new File(ManageSerializing.FILES_DIR + a.getFileName());
				if (!f.exists())
					createFile(f);
				lowLevelOut = new FileOutputStream(f);
				highLevelOut = new ObjectOutputStream(lowLevelOut);
				highLevelOut.writeObject(a);
				highLevelOut.flush();
				lowLevelOut.close();
			} catch (FileNotFoundException ex) {
				ex.printStackTrace();
				ok = false;
			} catch (IOException ex) {
				ex.printStackTrace();
				ok = false;
			} catch (NullPointerException ex) {
				ex.printStackTrace();
				ok = false;
			}
		} else
			ok = false;
	}

	public void writeFile(SerializableHawar a, String fileName) {
		if (a != null) {
			ok = true;
			a.setFileName(ManageSerializing.FILES_DIR + fileName);
			try {
				f = new File(ManageSerializing.FILES_DIR + fileName);
				if (!f.exists())
					createFile(f);
				lowLevelOut = new FileOutputStream(f);
				highLevelOut = new ObjectOutputStream(lowLevelOut);
				highLevelOut.writeObject(a);
				highLevelOut.flush();
				lowLevelOut.close();
			} catch (FileNotFoundException ex) {
				ex.printStackTrace();
				ok = false;
			} catch (IOException ex) {
				ex.printStackTrace();
				ok = false;
			} catch (NullPointerException ex) {
				ex.printStackTrace();
				ok = false;
			}
		} else
			ok = false;
	}

	/**
	 * Returns the ok.
	 * @return boolean
	 */
	public boolean isOk() {
		return ok;
	}

	/**
	 * Sets the ok.
	 * @param ok The ok to set
	 */
	public void setOk(boolean ok) {
		this.ok = ok;
	}

	private void createFile(File a) throws IOException {
		a.createNewFile();
	}
}
